<template>
  <nav class="site-navbar" :class="'site-navbar--' + navbarLayoutType">
    <div class="site-navbar__header">
      <h1 class="site-navbar__brand" @click="$router.push({ name: 'home' })">
        <a class="site-navbar__brand-lg" href="javascript:;">后台管理系统</a>
        <a class="site-navbar__brand-mini" href="javascript:;">智梦源</a>
      </h1>
    </div>
    <div class="site-navbar__body clearfix">

      <el-menu
        class="site-navbar__menu"
        mode="horizontal">
        <el-menu-item class="site-navbar__switch" index="0" @click="sidebarFold = !sidebarFold">
          <icon-svg name="zhedie"></icon-svg>
        </el-menu-item>
        <el-menu-item class="site-navbar__switch" index="0" @click="refresh()">
          <icon-svg name="shuaxin"></icon-svg>
        </el-menu-item>
      </el-menu>
      <el-menu
        class="site-navbar__menu site-navbar__menu--right"
        mode="horizontal">
        <!--        <el-menu-item index="1" @click="$router.push({ name: 'theme' })">-->
        <!--          <template slot="title">-->
        <!--            &lt;!&ndash;<el-badge value="new">&ndash;&gt;-->
        <!--              <icon-svg name="shezhi" class="el-icon-setting"></icon-svg>-->
        <!--            &lt;!&ndash;</el-badge>&ndash;&gt;-->
        <!--          </template>-->
        <!--        </el-menu-item>-->
        <el-menu-item index="1" class="site-navbar__switch" @click="goImsgLoguser">
          <el-tooltip class="item" effect="dark" :content="msessageList.length+'条未读消息'" placement="top-start">
            <el-badge :max="99" :value="msessageList.length" :hidden="msessageList.length == 0" class="item">
               <icon-svg is-dot name="tixing"></icon-svg>
            </el-badge>
          </el-tooltip>

        </el-menu-item>
        <el-menu-item index="1" class="site-navbar__switch">
          <!--<el-badge value="new">-->
          <div class="btn-fullscreen" @click="handleFullScreen">
            <el-tooltip effect="dark" :content="fullscreen?`取消全屏`:`全屏`" placement="bottom">
              <icon-svg :name="fullscreen?'cancelquanping':'quanping'"></icon-svg>
            </el-tooltip>
          </div>
          <!--</el-badge>-->
        </el-menu-item>

        <el-menu-item class="site-navbar__avatar" index="3">
          <el-dropdown :show-timeout="0" placement="bottom">
            <span class="el-dropdown-link">
              <img src="~@/assets/img/avatar.png" :alt="userName">{{ userName }}
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="updatePasswordHandle()">修改密码</el-dropdown-item>
              <el-dropdown-item @click.native="logoutHandle()">退出</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-menu-item>
      </el-menu>
    </div>
    <!-- 弹窗, 修改密码 -->
    <update-password v-if="updatePassowrdVisible" ref="updatePassowrd"></update-password>
  </nav>
</template>

<script>
  import UpdatePassword from './main-navbar-update-password'
  import {clearLoginInfo} from '@/utils'

  export default {
    inject: ['refresh'],
    data() {
      return {
        updatePassowrdVisible: false,
        fullscreen: false,
        msessageList: this.$store.state.websocket.msg
      }
    },
    components: {
      UpdatePassword
    },
    computed: {
      navbarLayoutType: {
        get() {
          return this.$store.state.common.navbarLayoutType
        }
      },
      sidebarFold: {
        get() {
          return this.$store.state.common.sidebarFold
        },
        set(val) {
          this.$store.commit('common/updateSidebarFold', val)
        }
      },
      mainTabs: {
        get() {
          return this.$store.state.common.mainTabs
        },
        set(val) {
          this.$store.commit('common/updateMainTabs', val)
        }
      },
      userName: {
        get() {
          return this.$store.state.user.name
        }
      }
    },
    methods: {
      // 修改密码
      updatePasswordHandle() {
        this.updatePassowrdVisible = true
        this.$nextTick(() => {
          this.$refs.updatePassowrd.init()
        })
      },
      // 退出
      logoutHandle() {
        this.$confirm(`确定进行[退出]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sys/logout'),
            method: 'post',
            data: this.$http.adornData()
          }).then(({data}) => {
            if (data && data.code === 0) {
              clearLoginInfo()
              this.$router.push({name: 'login'})
            }
          })
        }).catch(() => {
        })
      },
      // 全屏
      handleFullScreen() {
        let element = document.documentElement;
        if (this.fullscreen) {
          if (document.exitFullscreen) {
            document.exitFullscreen();
          } else if (document.webkitCancelFullScreen) {
            document.webkitCancelFullScreen();
          } else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
          } else if (document.msExitFullscreen) {
            document.msExitFullscreen();
          }
        } else {
          if (element.requestFullscreen) {
            element.requestFullscreen();
          } else if (element.webkitRequestFullScreen) {
            element.webkitRequestFullScreen();
          } else if (element.mozRequestFullScreen) {
            element.mozRequestFullScreen();
          } else if (element.msRequestFullscreen) {
            // IE11
            element.msRequestFullscreen();
          }
        }
        this.fullscreen = !this.fullscreen;
      },

      // 侧边栏折叠
      collapseChage() {
        this.sidebarFold = !sidebarFold
      },
      mounted() {
        if (document.body.clientWidth < 1500) {
          this.collapseChage();
        }
      },
      goImsgLoguser(){
        console.log("sda")
        this.$router.push("notice-imsgloguser")
      }
    }
  }
</script>
<style scoped>
  .btn-fullscreen {

    font-size: 20px;
  }
</style>
